package com.bluedot.bank.action;

import com.bluedot.bank.dao.Pagination;
import com.bluedot.bank.dao.account.Account;
import com.bluedot.bank.dao.account.AccountConditions;
import com.bluedot.bank.dao.account.AccountDao;
import com.bluedot.bank.dao.account.AccountDaoImpl;
import com.opensymphony.xwork2.ActionSupport;

public class FindAccountAction extends ActionSupport {
	private int filtername;
	private String c_value;
	private Pagination<Account> pagination = new Pagination<Account>();

	public int getFiltername() {
		return filtername;
	}

	public void setFiltername(int filtername) {
		this.filtername = filtername;
	}

	public String getC_value() {
		return c_value;
	}

	public void setC_value(String c_value) {
		this.c_value = c_value;
	}

	public Pagination<Account> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<Account> pagination) {
		this.pagination = pagination;
	}

	@Override
	public String execute() throws Exception {
		AccountDao accountDao = new AccountDaoImpl();
		switch (filtername) {
		case 1:
			pagesResult(pagination, null);
			accountDao.browse(pagination);
			break;
		case 2:
			pagesResult(pagination, getCondition("aIdFilter", "accountId",
					c_value));
			accountDao.browse(pagination);
			break;
		case 3:
			pagesResult(pagination, getCondition("cIdFilter",
					"customerId", c_value));
			accountDao.browse(pagination);
			break;
		case 4:
			pagesResult(pagination, getCondition("aTypeFilter",
					"typeKey", c_value));
			accountDao.browse(pagination);
			break;

		default:
			pagesResult(pagination, null);
		    accountDao.browse(pagination);
			break;
		}
		return SUCCESS;
	}

	void pagesResult(Pagination<Account> pagination, AccountConditions conditons) {
		if (this.pagination == null) {
			this.pagination = new Pagination<Account>();
		}

		if (this.pagination.getSize() == 0) {
			this.pagination.setSize(3);
		}

		if (this.pagination.getNo() == 0) {
			this.pagination.setNo(1);
		}
		pagination.setConditon(conditons);
	}

	AccountConditions getCondition(String filterName, String key, Object o) {
		AccountConditions conditons = new AccountConditions();
		conditons.setFilterName(filterName);
		conditons.setKey(key);
		conditons.setO(o);
		return conditons;
	}

}
